var utils = require('../../../util/utils');
var User = require('../../login/lib/userSchema');
var logger = require('pomelo-logger').getLogger('area-Remote', 'areaRemote');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.joinChannel = function (uid, frontendId, cb) {
    var channelService = this.app.get('channelService');
    var channel = channelService.getChannel('total', true);
    channel.add(uid, frontendId);
    utils.invokeCallback(cb);
};

handler.leaveChannel = function (uid, frontendId, cb) {
    var channelService = this.app.get('channelService');
    var channel = channelService.getChannel('total', true);
    channel.leave(uid, frontendId);
    utils.invokeCallback(cb);
};

//将消息推送给客户端
handler.pushMessageToClient = function (uid, frontendId, route, data, cb) {
    var self = this;
    self.app.get('channelService').pushMessageByUids(route, data, [{
        uid: uid,
        sid: frontendId
    }], null, function (err) {
        if (!!err) {
            logger.error("err: ", err)
        }
    });
    utils.invokeCallback(cb);
};
